package com.dyrj.ysk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dyrj.ysk.exception.BizException;
import com.dyrj.ysk.service.SignService;
import com.dyrj.ysk.util.JSONSignUtil;
import com.dyrj.ysk.util.RsaUtils;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * 加签验签服务
 * @author 吴侠志  2019/10/18
 * @version 1.0
 */
@Service
public class SignServiceImpl implements SignService {
    @Override
    public String sign(Map<String, String> params, String privateKey) throws BizException {
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(params));
        try {
            return RsaUtils.sign(jsonObject, privateKey);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException(BizException.ErrorCode.SIGN_ERROR);
        }
    }

    @Override
    public boolean verify(Map<String, String> params, String sign, String publicKey) throws BizException {
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(params));
        try {
            return RsaUtils.check(jsonObject, sign, publicKey);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException(BizException.ErrorCode.VIRIFY_ERROR);
        }
    }
}
